package com.xmall.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.annotation.LoginRequiredFalse;
import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.base.ResultCode;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.login.UserDto;
import com.xmall.sys.define.dto.user.SysUserDto;
import com.xmall.sys.define.request.user.UserListPageQueryRequest;
import com.xmall.sys.define.request.user.UserRoleUpdateRequest;
import com.xmall.sys.define.request.user.UserUpdateRequest;
import com.xmall.sys.service.IUserService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户
 */
@RequestMapping("/service/sys/user")
@RestController
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 用户关联的角色ID列表
     */
    @GetMapping("/roleIds/{userId}")
    public Result<List<Long>> userRoleIds(@PathVariable("userId") Long userId){
        List<Long> userRolesIds = userService.userRoleIds(userId);
        return Result.success(userRolesIds);
    }

    /**
     * 更新用户关联的角色列表
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:user:role')")
    @PostMapping("/updateUserRole")
    public Result updateUserRole(@Validated @RequestBody UserRoleUpdateRequest request){
        userService.updateUserRole(request);
        return Result.success();
    }

    /**
     * 用户权限信息
     */
    @LoginRequiredFalse
    @GetMapping("/infoWithPermission/{userId}")
    public Result<UserDto> userPermission(@PathVariable("userId") Long userId){
        UserDto userDto = userService.userPermission(userId);
        return null != userDto ? Result.success(userDto) : Result.error(ResultCode.NOT_EXIST);
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<SysUserDto> info(@PathVariable("id") Long id){
        SysUserDto sysUserDto = userService.info(id);
        return null != sysUserDto ? Result.success(sysUserDto) : Result.error(ResultCode.NOT_EXIST);
    }

    /**
     * 分页列表
     */
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    @GetMapping("/listPage")
    public Result<Page<SysUserDto>> listPage(@Validated UserListPageQueryRequest query){
        Page<SysUserDto> pageList = userService.listPage(query);
        return Result.success(pageList);
    }

    /**
     * 保存
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('sys:user:save')")
    @PostMapping("/save")
    public Result saveEntity(@Validated(value = AddGroup.class) @RequestBody UserUpdateRequest request){
        userService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:user:update')")
    @PostMapping("/update")
    public Result updateEntity(@Validated(value = UpdateGroup.class) @RequestBody UserUpdateRequest request){
        userService.updateEntity(request);
        return Result.success();
    }
}
